package com.shujia.kafka;


import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class Demo01KafkaProducer {
    public static void main(String[] args) {

        Properties prop = new Properties();
        // 设置Kafka Broker的地址
        prop.setProperty("bootstrap.servers","master:9092,node1:9092,node2:9092");
        prop.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        prop.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<>(prop);

        // 发送数据
        // 如果topic不存在则会自动创建
        String topic = "t1";
        producer.send(new ProducerRecord<>(topic,"1"));
        producer.send(new ProducerRecord<>(topic,"2"));
        producer.send(new ProducerRecord<>(topic,"3"));
        producer.send(new ProducerRecord<>(topic,"4"));

        // 将数据刷到磁盘
        producer.flush();

    }
}
